const Mock = require('mockjs');

import {dataA, dataB, dataC} from '../../api/group' 

/**
 * 使用MOCK生成模拟数据
 * Mock.mock(url, dataTmp)
 * */
Mock.mock('group_a', dataA);
Mock.mock('group_b', dataB);
Mock.mock('group_c', dataC);

const state = {
    group_a: [],
    group_b: [],
    group_c: []
}

const mutations = {
    getGroup (state, payload) {
        switch (payload.tag) {
            case 'a':
                state.group_a = payload.result;
                break;
            case 'b':
                state.group_b = payload.result;
                break;
            case 'c': 
                state.group_c = payload.result;
                break;
            default: 
                break;                
        }
    }
}

const actions = {
    getGroup ({commit, state, rootGetters}, payload) {
        rootGetters.$ax.get('group_a')
        .then(function(result){
            commit('getGroup', {
                tag: 'a',
                result: result.data
            })
        }).catch(function(err){
            console.log(err);
        })

        rootGetters.$ax.get('group_b')
        .then(function(result){
            commit('getGroup', {
                tag: 'b',
                result: result.data
            })
        }).catch(function(err){
            console.log(err)
        })

        rootGetters.$ax.get('group_c')
        .then(function(result){
            commit({
                type: 'getGroup',
                tag: 'c',
                result: result.data
            })
        })

    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}